Public Class frmMain

    Private Sub showPanel(ByVal pnlMyPanel As Object)
        ' Purpose
        '   When users click a link on the left side, then hide what the
        '   user doesn't want and show what the user wants

        If pnlMyPanel.visible = True Then
            ' If panel is already being displayed, don't do anything
        Else
            ' Update header
            UpdateConfiguring(pnlMyPanel.tag)

            ' Hide all the other panels
            Me.pnlBasic.Hide()
            Me.pnlTCL.Hide()
            Me.pnlViewConnection.Hide()
            Me.pnlViewImportExport.Hide()
            Me.pnlViewManual.Hide()
            Me.pnlViewShellConfig.Hide()

            ' Show the panel we want to show
            pnlMyPanel.Left = 172
            pnlMyPanel.Top = 133

            pnlMyPanel.show()
            pnlMyPanel.BringToFront()
            pnlMyPanel.parent = Me
        End If
    End Sub

    Private Sub llblViewBasicSettings_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llblViewBasicSettings.LinkClicked
        ' Purpose:
        '   This subroutine is executed when the user clicks the 
        '   "Basic Settings..." link on the left

        showPanel(Me.pnlBasic)
    End Sub

    Private Sub UpdateConfiguring(ByVal NewTitle As String)
        ' Purpose:
        '   Generic object handler for when an item on the left side of the
        '   window is clicked.
        ' Postconditions:
        '   text below "Currently Editing" is updated

        Me.lblCurrentPanel.Text = NewTitle
    End Sub

    Public Sub UpdateTCLList()
        ' Purpose:
        '   Updates the list of TCL Scripts the user wishes to use
        ' Preconditions:
        '   none
        ' Postconditions:
        '   TCL Scripts are all listed in a checklist box.
        '   A static variable is set so we don't do this again by accident

        Dim strAllScripts As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
        Dim strDirectoryToAccess As String
        Dim intLoopCounter As Integer
        Dim intHighestScriptIndex As Integer
        Dim strItemName As String
        Static bListUpdated As Boolean

        ' Only list items in the checkbox list once
        If bListUpdated = False Then
            ' Disable edit button until needed
            butEditTCL.Enabled = False

            ' Set directory to access to TCL script path
            strDirectoryToAccess = XBotNet.gStrTCLScriptDir

            ' Get all TCL files in that directory
            strAllScripts = My.Computer.FileSystem.GetFiles(strDirectoryToAccess, FileIO.SearchOption.SearchTopLevelOnly, "*.tcl")

            ' Store the highest script index
            intHighestScriptIndex = strAllScripts.Count - 1

            ' Loop through all TCL Scripts
            For intLoopCounter = 0 To intHighestScriptIndex
                ' TODO: determine when items should be checked or not

                ' Get the name of the item
                strItemName = strAllScripts.Item(intLoopCounter)

                ' Remove the directory...
                strItemName = Replace(strItemName, strDirectoryToAccess & "\", "")

                ' Remove the extension
                strItemName = Replace(strItemName, ".tcl", "")

                ' Add it to the checkbox list
                chkTCLScripts.Items.Add(strItemName, False)
            Next
            chkTCLScripts.Refresh()
        End If

        ' Note we already updated this list
        bListUpdated = True
    End Sub

    Private Sub llblViewTCLScripts_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llblViewTCLScripts.LinkClicked
        ' Purpose:
        '   This subroutine is executed when the user clicks the 
        '   "TCL Scripts..." link on the left

        ' Display the TCL Panel
        showPanel(Me.pnlTCL)

        ' Resize everything as appropriate
        UpdateTCLDisplay()

        ' Update the list of scripts as necessary
        UpdateTCLList()
    End Sub

    Private Sub SplitContainer1_SplitterMoved(ByVal sender As Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles TCLSplitContainer.SplitterMoved
        ' Purpose:
        '   This subroutine is executed when the user resizes the splitter
        '   in the TCL Panel
        UpdateTCLDisplay()
    End Sub


    Private Sub chkTCLScripts_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkTCLScripts.SelectedValueChanged
        ' Purpose:
        '   This subroutine is executed when the user clicks on a different script

        ' Display information by reading from .desc file

        Dim intFileNo As Integer
        Dim strFileLineContents As String
        Dim strFileToRead As String
        intFileNo = Microsoft.VisualBasic.FreeFile()

        ' Figure out what we need to read...
        strFileToRead = XBotNet.gStrTCLScriptDir & "\" & chkTCLScripts.SelectedItem.ToString & ".desc"

        ' If file exists, read it
        If My.Computer.FileSystem.FileExists(strFileToRead) Then
            ' Open the file but continue to let any other app modify it as we just need to quickly read it
            Microsoft.VisualBasic.FileOpen(intFileNo, strFileToRead, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)

            ' Get the title
            strFileLineContents = Microsoft.VisualBasic.LineInput(intFileNo)
            Me.lblTCLScriptName.Text = strFileLineContents

            ' Get the rest
            Me.txtTCLDesc.Clear()
            Do Until Microsoft.VisualBasic.EOF(intFileNo)
                strFileLineContents = Microsoft.VisualBasic.LineInput(intFileNo)
                Me.txtTCLDesc.Text = Me.txtTCLDesc.Text & strFileLineContents & vbCrLf
            Loop

            ' Close the file
            Microsoft.VisualBasic.FileClose(intFileNo)
        Else
            ' No description file exists, assume user added it to the library manually
            lblTCLScriptName.Text = chkTCLScripts.SelectedItem.ToString & ".TCL"
            txtTCLDesc.Text = "No description (.DESC) file was found for this script"
        End If

        ' Re-enable edit button since now needed
        butEditTCL.Enabled = True
    End Sub


    Private Sub butEditTCL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butEditTCL.Click
        ' Purpose:
        '   Executes when the user clicks the button to edit the TCL
        Dim strFileToRead As String

        ' Generate the file we need to read
        strFileToRead = XBotNet.gStrTCLScriptDir & "\" & chkTCLScripts.SelectedItem.ToString & ".tcl"

        ' Open the file in Wordpad (Write)
        Microsoft.VisualBasic.Shell("write.exe " & strFileToRead, AppWinStyle.NormalFocus, False)
    End Sub

    Private Sub UpdateTCLDisplay()
        ' Purpose:
        '   Updates the display of the TCL splitter panel (or initializes it)

        ' Checklist of TCL Scripts
        Me.chkTCLScripts.Width = TCLSplitContainer.SplitterDistance

        ' Import button
        Me.butImportTCLScript.Width = Me.chkTCLScripts.Width

        ' TCL Description button
        Me.txtTCLDesc.Width = TCLSplitContainer.Size.Width - TCLSplitContainer.SplitterDistance - TCLSplitContainer.SplitterWidth

        ' Edit TCL button
        Me.butEditTCL.Width = Me.txtTCLDesc.Width

        ' TCL Script Title does not need to be edited as it autosizes
    End Sub

    Private Sub llblViewSecurityOptions_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llblViewConnectionOptions.LinkClicked
        ' Purpose:
        '   This subroutine is executed when the user clicks the 
        '   "Security Options..." link on the left

        ' Display the Connection Panel
        showPanel(Me.pnlViewConnection)
    End Sub

    Private Sub llblViewImportExport_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llblViewImportExport.LinkClicked
        ' Purpose:
        '   This subroutine is executed when the user clicks the 
        '   "Import/Export Options..." link on the left

        ' Display the Import/Export Panel
        showPanel(Me.pnlViewImportExport)
    End Sub

    Private Sub llblViewManualEdit_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llblViewManualEdit.LinkClicked
        ' Purpose:
        '   This subroutine is executed when the user clicks the 
        '   "Manual Edit..." link on the left

        ' Display the Manual Edit Panel
        showPanel(Me.pnlViewManual)
    End Sub

    Private Sub lblViewRunOnShell_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lblViewRunOnShell.LinkClicked
        ' Purpose:
        '   This subroutine is executed when the user clicks the 
        '   "Run Bot on Shell Server..." link on the left

        ' Display the Manual Edit Panel
        showPanel(me.pnlViewShellConfig)
    End Sub

End Class
